package org.bugjail.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.AccessLevel;
import org.bugjail.data.Person;
import org.bugjail.data.Project;
import org.bugjail.servlet.language.LanguageStringLoader;

/**
 * @author Felipe Melo
 * 
 * Servlet implementation class ProjectServlet
 * 
 */
public class ProjectServlet extends BasicUIServlet {
	
	static final long serialVersionUID = 1L;
	
	@Override
	protected void write(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Person user = ServletUtilities.getLogedUser(request);
		
		PrintWriter pw = response.getWriter();		

		pw.println("<table width='100%'><tr>");
		pw.println("<td align=left></td>");
		pw.println("<td align=right><a href='NewProjectFormServlet'><img src='images/new.png' /> Create new project</a></td>");
		pw.println("</tr></table>");
		//pw.println("<br>");
		
		pw.println("<table width='100%'>");
		pw.println("<tr><td width='100%'><table class='hometable'>");			
		
		Map<Project, AccessLevel> projects = user.getProjects();
		if(projects != null && projects.size() > 0){
			pw.println("<tr><th>Name</th><th>Description</th><th width=40 colspan=2/></tr>");
			for (Project project : projects.keySet()) {
				pw.println("<tr>" +
						//"<td><a href='TestDesignServlet?project="+projects.get(i).getId()+"'>"+ projects.get(i).getName() +"</a></td>" +
						"<td><a href='SelectProjectServlet?project="+project.getId()+"'>"+ project.getName() +"</a></td>" +
					     "<td>"+ project.getDescription() +"</td>" +
						     "<td width=20><img src='images/edit.png' /></td>" +
						     "<td width=20><img src='images/delete.png' /></td>" +
						   "</tr>");
			}
		} else {
			pw.println("<tr><th>- There is no Project associated to this User.</th></tr>");
		}
		
		pw.println("</table></td>");
		pw.print("</tr></table>");
		pw.print("<br>");
	}

	@Override
	protected String pageTitle() {
		return LanguageStringLoader.loadString(LanguageStringLoader.STR_PROJECTS, getLoggedUser().getLanguage(), "Projects");
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
	}

}
